import { setToken, getToken, reomveToken } from '../../plugins/cookie';
import { getClassifyList, addClassifyList, updatedClassifyList, deleteClassifyList, getLogin } from '../../api/audit/index';
const state = () => {
    return {
        list: [],
        tableData: [],
        roles: [],
        userInfo: {}
    }
}

const mutations = {
    SET_LIST(state, arr) {
        state.list = arr
    },
    SET_CLASSIFY(state, arr) {
        state.tableData = arr
    },
    SET_ROLES(state, roles) {
        state.roles = roles
    },
    SET_USERINFO(state, data) {
        state.userInfo = data
    },
    // 退出
    DELETELIST(state) {
        state.roles = []
        state.userInfo = {}
        reomveToken()
    }
}

const actions = {
    // 根据 token 获取用户相关信息
    async getUserInfo({ commit }, token) {
        const { data } = await this.$axios.get("user/info?token=" + token)
        commit("SET_ROLES", data.data.roles.split(","))
        commit("SET_USERINFO", data.data)
    },

    // 登录验证
    async doLogin({ }, form) {
        const { data } = await getLogin(form)
        setToken(data.token)
    },
    // 获取用户列表
    async getList({ commit }) {
        const { data } = await this.$axios.get("msbd/list");
        commit("SET_LIST", data.data)
    },


    /* ********************************************************** */

    // 获取类别列表
    async getClassify({ commit }) {
        const { data } = await this.$axios.get("classify/list")
        commit("SET_CLASSIFY", data.data)
    },
    // 新增类别
    async addClassify({ dispatch }, classify) {
        await addClassifyList({ name: classify })
        dispatch("getClassify")
    },
    // 删除类别
    async removeClassify({ dispatch }, row) {
        await deleteClassifyList(row)
        dispatch("getClassify")
    },
    // 修改类别
    async updatedClassify({ dispatch }, row) {
        await updatedClassifyList(row)
        dispatch("getClassify")
    }
}

const getters = {

}

export {
    state,
    mutations,
    actions,
    getters
}